<?php
/**
 * 数据层基类 
 */
class framework_base_model {

    protected $_name = "";
    //table name

    public static function DB() {
        $pdo = framework_base_pdomysql::connect();
        return $pdo;
    }

    /**
     * [column] = value value要处理
     * @param array $data
     * @return mixed
     */
    public function insert(array $data) {
        $sql = "INSERT INTO " . $this -> _name . " (`" . implode('`, `', array_keys($data)) . "`,raw_add_time) " .
                "VALUES ('" . implode('\',\'', array_values($data)) . "',now())";
        //echo $sql;
        if (FIRE_PHP) {
            try {
                FirePHP::getInstance(true) -> log($sql);
            } catch (Exception $e) {

            }
        }
        self::DB() -> exec($sql);
        if ($lastInsertId = self::DB() -> lastInsertId()) {
            $log['sql'] = addslashes($sql);
            model_logsql::getInstance() -> save($log);
        }

        return $lastInsertId;
    }

    /**
     * 单表更新
     * @param $data
     * @param $where
     * @return mixed
     */
    public function update($data, $where) {
        $sql = "UPDATE " . $this -> _name . " SET ";
        foreach ($data as $col => $value) {
            $sql .= "`" . $col . "` = '" . $value . "',";
        }
        $sql = trim($sql, ",") . " WHERE ";
        foreach ($where as $col => $value) {
            $sql .= "`" . $col . "` = '" . $value . "' AND ";
        }
        $sql = trim($sql, "AND ");
        if (FIRE_PHP) {
            try {
                FirePHP::getInstance(true) -> log($sql);
            } catch (Exception $e) {

            }
        }

        if ($affectedRows = self::DB() -> exec($sql)) {
            $log['sql'] = addslashes($sql);
            model_logsql::getInstance() -> save($log);
        }

        return $affectedRows;
    }

    /**
     * 单表分页查询
     * @param array $data
     * @return mixed
     */
    public function paginator(array $data) {
        foreach ($data['col'] as $col) {
            $cols[] = "`" . $col . "`";
        }

        $sql = "SELECT COUNT(*) FROM " . $this -> _name;
        if (isset($data['where'])) {
            $sql .= " WHERE ";
            foreach ($data['where'] as $w) {
                $sql .= $w . " AND ";
            }
            $sql = trim($sql, " AND ");
        }
        if (FIRE_PHP) {
            try {
                FirePHP::getInstance(true) -> log($sql);
            } catch (Exception $e) {

            }
        }
        $sth = self::DB() -> query($sql);
        $count = $sth -> fetchColumn();
        $sql = strtr($sql, array('COUNT(*)' => implode(', ', $cols)));

        if (isset($data['order'])) {
            $sql .= " ORDER BY ";
            foreach ($data['order'] as $col => $order) {
                $sql .= $col . " " . $order . " ,";
            }
            $sql = trim($sql, " ,");
        }
        if (isset($data['page'])) {
            $sql .= " LIMIT " . ($data['page']['current_page'] - 1) * $data['page']['page_size'] . "," . $data['page']['page_size'];
        }
        if (FIRE_PHP) {
            FirePHP::getInstance(true) -> log($sql);
        }
        $sth = self::DB() -> query($sql);
        $result['count'] = $count;
        $result['page_size'] = $data['page']['page_size'];
        $result['records'] = $sth -> fetchAll();
        return $result;
    }

    /**
     * 单表select
     * @param $data
     * @param string $type 如果是all ,则返回二唯数组
     * @return mixed
     */
    public function select($data, $type = "single") {
        $sql = "SELECT " . implode(',', $data['col']) . " FROM " . $this -> _name;

        if (isset($data['where'])) {
            $sql .= " WHERE ";
            foreach ($data['where'] as $w) {
                $sql .= $w . " AND ";
            }
            $sql = trim($sql, " AND ");
        }
        if (FIRE_PHP) {
            try {
                FirePHP::getInstance(true) -> log($sql);
            } catch (Exception $e) {

            }
        }
        $sth = self::DB() -> query($sql);

        if ($type == "all") {
            return $sth -> fetchAll();
        } else {
            return $sth -> fetch(PDO::FETCH_ASSOC);
        }
    }

    /**
     * @param $sql SQL语句
     * @return mixed
     */
    public function fetchAll($sql)
    {
        if (FIRE_PHP) {
            try {
                FirePHP::getInstance(true) -> log($sql);
            } catch (Exception $e) {

            }
        }
        $sth = self::DB()->prepare($sql);
        $sth->execute();
        return $sth->fetchAll();
    }

    /**
     * 返回一纬数组
     * @param $sql
     * @return mixed
     */
    public function fetchRecord($sql) {
        if (FIRE_PHP) {
            try {
                FirePHP::getInstance(true) -> log($sql);
            } catch (Exception $e) {

            }
        }
        $sth = self::DB() -> query($sql);
        return $sth -> fetch(PDO::FETCH_ASSOC);
    }

}
